let bid=28;
let TypeLink = [];
let pageNo=1,pageSize=10;
let form,layer,laydate,upload,element,table,laypage,colorpicker;
let typeList;
let updateFlag=false;//是否改变该板块内容
let type=0,tid=1;
layui.config({
    base: '../../Scripts/layui/lay/modules/'
}).use(['form','laydate', 'layer', 'element','upload',"table"],function(){
    form = layui.form;
    layer = layui.layer;
    laydate = layui.laydate;//日期
    table = layui.table;//日期
    laypage = layui.laypage;
    upload = layui.upload;//上传
    colorpicker=layui.colorpicker;
    element = layui.element; //元素操作
    selectRightNav();//查询右边导航栏
    $(".nav-tabs-custom .navli").on("click",function () {
        //提示是否切换，如果切换，之前的数据将不保留
        if(updateFlag){
            layer.confirm("请先提交改板块的设置，切换后，将不会保存您之前的设置!", {icon: 3, title: '提示信息'}, function (index) {
                changeTab($(this));
            });
        }else{
            changeTab($(this));
        }
    })
});
$(function () {
    $.ajax({
        url: httpurl + "/selectCpikeTemplateContentByTid",
        type: 'GET',
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            tid: tid
        },
        success: function (data) {
            if (data.rtnCode == 200) {
                a = JSON.parse(data.data.content);
                _.each(a.PModules,
                    function (a, b) {
                        var c = 0 == b ? !0 : !1;
                        HiShop.DIY.add(a, c);
                    });
                _.each(a.LModules,
                    function (a) {
                        HiShop.DIY.add(a);
                    });
                if ($(".j-pagetitle-icon").length > 0) {
                    $("#IconUrlBox").himallUpload(
                        {
                            title: '',
                            headerWidth: 0,
                            dataWidth: 10,
                            imageDescript: '建议700 * 360',
                            displayImgSrc: a.page.icon,
                            imgFieldName: "IconUrlPic",
                            callback: function (data) {
                                $(".j-pagetitle-icon").val(data);
                            }
                        });
                }
            }
        }
    }),
    $("#j-savePage").click(function () {
        callback();
    })
});
function gotolastInfo() {
    window.location.href="VHomepage.html?typeId="+typeId;
}
var xhr;
let savei=0;
function sureSetting() {
    let dom = document.getElementById("diy-contain");　//要转化的div
    let width = dom.offsetWidth;
    let height = dom.offsetHeight;
    html2canvas(dom,{
        logging: false, //日志开关，便于查看html2canvas的内部执行流程
        windowWidth:width,
        windowHeight:height,
        x:0,
        useCORS: true
    },{
        backgroundColor: null//无背景
    }).then((canvas) => {
        console.log("模板图片生成中......");
        let pic = canvas.toDataURL();
        let indexOf = pic.indexOf(",");
        if(indexOf>=-1){
            pic=pic.split(",")[1];
        }
        $.ajax({
            type:"POST",
            url: httpurl+"/uploadBaseQiniu",
            cache: false,
            success: function (data) {
                if(data.rtnCode==200){
                    savei=0;
                    let token=data.data;
                    var url = "http://upload-z2.qiniup.com/putb64/-1";
                    xhr = new XMLHttpRequest();
                    xhr.onreadystatechange=callback;
                    xhr.open("POST", url, true);
                    xhr.setRequestHeader("Content-Type", "application/octet-stream");
                    xhr.setRequestHeader("Authorization", "UpToken "+token);
                    xhr.send(pic);
                }
            }, error: function (data) {
                top.layer.alert("保存图片更新失败！！", function (index) {
                    layer.close(index);
                });
            }
        });
    });
}

function callback() {
    return HiShop.DIY.Unit.getData() ? ($.ajax({
        url: httpurl + "/updateCpikeTemplateContent",
        type: "POST",
        // dataType: "json",
        data: JSON.stringify({
            tid: tid,
            // img: visitimgurl+"/"+key,
            content: JSON.stringify(HiShop.DIY.Unit.getData()),
            // startTime: startTime,
            // endTime: endTime,
            // title: activityName,
            // note: note,
            // enable: enable,
            // defaulti: defaulti,
            // type: typeId,
            // sort: sort,
        }),
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        beforeSend: function () {
            $.jBox.showloading()
        },
        success: function (a) {
            if (200 == a.rtnCode) {
                HiShop.hint("success", "恭喜您，保存成功！");
                setTimeout(function () {
                    window.location.href = "VHomepage.html?typeId="+typeId;
                }, 1000);
                if (c == 11 || c == 12 || c == 15 || c == 16) {
                    window.opener.location.reload();
                    $("#vtclient").val(a.tname);
                    //if (b.length == 0 || b == "" || b == "0") {
                    setTimeout(function () {
                        window.location.href = a.link;
                    }, 1000);
                    //}
                }
            } else {
                HiShop.hint("danger", "对不起，保存失败：" + a.msg)
            }
            $.jBox.hideloading();
            //处理专题新增的关闭
        }
    }), !1) : void 0

}

function changeTab(obj) {
    $(".nav-tabs-custom .navli").removeClass("active");
    $(obj).addClass("active");
    initMainContentList();
}
function initMainContentList() {
    $("#diy-contain").html("");
    $("#diy-ctrl").html("");
}
function saveImg(name,file,type) {
    $.ajax({
        async:false,
        url: httpurl+HiShop.Config.AjaxUrl.saveImg,
        type: 'POST',
        data: {
            name: name,
            file: file,
            type: type,
        },
        success: function (data) {

        }
    })
}
function selectRightNav() {
    var info = "";
    $.get('../../Api/seckillTemplate.json', function (res) {
        for (let i = 0; i < res.length; i++) {
            info += '<div class="tab"><a href="javascript:;" class="j-diy-addModule" data-type="' + res[i].type + '">' + res[i].name + '</a></div>';
        }
        $("#addModules").html(info);
        $.getScript("../../Content/Public/plugins/diy/diy.init.js?v=11.110");
    });
}

// function selectTypeLink() {
//     $.get('../../Api/TypeLink.json', function (res) {
//         TypeLink = res;
//         $.getScript("../../Content/Public/js/dist/component-min.js");
//     });
// }

$(document).ready(function () {
    if ($('#j-initdata').val() == 0) {
        $('#j-savePage').click();
    }
    ;
    //      // 控制添加商品的图片显示高度，确保商品布局正常
    //      $('.b_mingoods,.mingoods').each(function (index, el) {
    //          var me = $(this),
    //              imgHeight = me.find('img').width();
    //          me.find('img').closest('a').height(imgHeight);
    //      });
    //      $('.board3').each(function (index, el) {
    //          var me = $(this);
    //          var bwidth = me.width();
    //          if (me.hasClass('small_board') || !me.hasClass('big_board')) {
    //              me.children('span').attr('style', 'height:' + bwidth + 'px !important;overflow:hidden;');
    //          }
    //          if (me.hasClass('big_board')) {
    //              me.children('span').attr('style', 'height:' + (bwidth * 2 + 10) + 'px !important;overflow:hidden;');
    //          }
    //      });

    //添加模块
    $('.tab').each(function () {
        $(this).children('a').mouseover(function () {
            $(this).children('a img').show();
            $(this).children('a b').hide();
        }).mouseout(function () {
            $(this).children('b').show();
            $(this).children('img').hide();
        });
    });
});
$(function () {
    $(".j-copy").zclip({
        path: '/Public/plugins/zclip/ZeroClipboard.swf',
        copy: function () {
            return $(this).data('copy');
        },
        afterCopy: function () {
            HiShop.hint("success", "内容已成功复制到您的剪贴板中");
        }
    });
    $(".btn-notice").click(function () {
        // $.post('/System/readAllNotice',{},function(){
        //     window.location.reload();
        // })
        $.ajax({
            url: '/System/readAllNotice',
            type: 'POST',
            success: function (data) {
                if (data.status == 1) {
                    window.location.reload();
                } else {
                    HiShop.hint("danger", data.msg);
                }

            }
        })
    });


    ;(function () {
        // 首页竖线到底
        var height1 = $(".content-right").height();
        var height2 = $(".content-left").height();
        if (parseInt(height1) < parseInt(height2)) {
            $(".content-right").css({'min-height': height2});
        }
        ;

    })();

});
$(function () {
    window.setTimeout(function () {
        gggoup();
    }, 5000);
    $('.gound_close').click(function () {
        $('#gonggao').animate({bottom: "-270px"}, 1000);
    });
});

function gggoup() {
    $('#gonggao').animate({bottom: "3px"}, 1000);
};

//拖拽
function dragAnimate(obj) {
    var ispeed = 200;
    obj.animate({
        left: $(window).width() - obj.width()
    }, ispeed);
}

$(function () {
    var oWin = $(window);
    var disX = 0;
    var disY = 0
    var oMargin = $('.diy-actions').height() / 2;
    //var oMargin = Math.abs(parseInt( $('.diy-actions').outerHeight(true) - $('.diy-actions').outerHeight() ));
    //console.log(oMargin);

    $('.diy-actions-drag').mousedown(function (ev) {
        disX = ev.pageX - $(this).offset().left;
        disY = ev.pageY - $(this).offset().top;
        $('.diy-actions').addClass('diy-actions-shadow');
        $('html').mousemove(function (ev) {
            var scrollTop = oWin.scrollTop();
            var t = ev.pageY - disY - scrollTop + oMargin;
            var l = ev.pageX - disX - $('.diy-actions-fold').width();
            var w = oWin.width() - $('.diy-actions').width();
            var h = oWin.height() - $('.diy-actions').height() + oMargin;
            if (t < oMargin) {
                t = oMargin;
            } else if (t > h) {
                t = h;
            }
            if (l < 0) {
                l = 0;
            } else if (l > w) {
                l = w;
            }
            $('.diy-actions').css({
                'top': t,
                'left': l,
            });
        });
        $('html').mouseup(function () {
            $('html').off();
            $('.diy-actions').removeClass('diy-actions-shadow');
            if (oWin.width() - $('.diy-actions').width() - $('.diy-actions').offset().left <= 80) {
                dragAnimate($('.diy-actions'));
            }
        });
        return false;
    });
    //点击切换到右边
    $('.diy-actions-fold').on('click', function () {
        dragAnimate($('.diy-actions'));
    });
});